﻿package org.leles.utils 
{
	import flash.display.DisplayObject;
	/**
	 * ...
	 * @author Bruno Leles
	 */
	public final class Aligner
	{
		public static function verticalAlign(... args):void 
		{
			aligner( args, 'x', 'width' );
			//var i:int		= 0,
				//n:int		= args.length,
				//p:Number	= evaluateValue( args[0], 'x' );
				//
			//var o:DisplayObject;
			//var d:Number;
			//
			//do
			//{
				//setPropertyIfNecessary( args[i], 'x', p );
				//p += evaluateValue( args[i], 'width' );
				//
				//i++;
			//}while ( i < n );
		}
		
		public static function horizontalAlign(... args):void 
		{
			aligner( args, 'y', 'height' );
			//var i:int		= 0,
				//n:int		= args.length,
				//p:Number	= evaluateValue( args[0], 'x' );
				//
			//var o:DisplayObject;
			//var d:Number;
			//
			//do
			//{
				//setPropertyIfNecessary( args[i], 'x', p );
				//p += evaluateValue( args[i], 'width' );
				//
				//i++;
			//}while ( i < n );
		}
		
		public static function aligner(vector:*, anchorProp:String, dimensionProp:String):void 
		{
			var i:int		= 0,										// pointer index
				n:int		= vector.length,							// length of array or vector
				p:Number	= evaluateValue( vector[0], anchorProp );	// current Point ( reference to place elements )
				
			while ( i < n );
			{
				setPropertyIfNecessary( vector[i], anchorProp, p );
				p += evaluateValue( vector[i], dimensionProp );
				i++;
			};
		}
		
		private function evaluateValue( elem:*, prop:String):Number 
		{
			if( elem is DisplayObject )
				return elem[prop] as Number;
			return elem as Number;
		}
		
		private function setPropertyIfPossible(elem:*, prop:String, value:Number):Number
		{
			try {
				elem[prop] = value;
			}catch (e:Error) { }
			return value;
		}
	}

}